package com.itheima.Jiuye14;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
//第二题：分析以下需求，并用代码实现
//有100份礼品,两人同时发送，当剩下的礼品小于10份的时候则不再送出，
//利用多线程模拟该过程并将线程的名称打印出来
public class Zuoye2 implements Runnable{
    private Lock lock = new ReentrantLock();
    private int a = 100;
    @Override
    public void run() {
         while (true){
             try {
                 lock.lock();
                 if (a <=10){
                     break;
                 }else {
                     a--;
                     System.out.println(Thread.currentThread().getName()+"在发放小礼物,还剩"+a+"个小礼物");
                 }
             } finally {
                 lock.unlock();
             }
         }
    }
}
